electron = require('electron')
mysql = require('./nccPkg/mysql/index')
window.mysql = mysql.default;
console.log(utools)
console.log(window.mysql)
window.uilt = {}
window.uilt.onPluginReady = async function () {
    await utools.onPluginReady()
}
window.uilt.ipcRenderer = electron.ipcRenderer;
// 创建窗口方法
let createWindow = function () {
    window.ubWindow = utools.createBrowserWindow(
"../code/vue_test.html",
        {
            // window.ubWindow = utools.createBrowserWindow('html/index.html', {
            show: false,
            title: "码农专用SQL工具",
            width: 1280,
            height: 700,
            frame: false,
            nodeIntegration: true,
            //使用远程模块必须开启此处
            enableRemoteModule: true,
            webPreferences: {
                preload: "preload.js",
            },
        }, () => {
            // 显示
            window.ubWindow.show()
window.ubWindow.webContents.openDevTools();
            // 置顶
            // ubWindow.setAlwaysOnTop(true)
            // 窗口全屏
            // ubWindow.setFullScreen(true)
            // 执行脚本
            // ubWindow.executeJavaScript('fetch("https://jsonplaceholder.typicode.com/users/1").then(resp => resp.json())')
            //   .then((result) => {
            //     console.log(result) // Will be the JSON object from the fetch call
            //   })
        })
}
// 注册最大化最小化和关闭窗口方法
let IntervalEvent = null;
let registerEvent = () => {
    // 向子窗口传递心跳
    // window.uilt.ipcRenderer.removeAllListeners('closeSqlWindow');
    console.log('ipcRenderer 开始监听')
    // setTimeout(() => {
    window.uilt.ipcRenderer.removeAllListeners('closeSqlWindow').on('closeSqlWindow', (event, data) => {
        console.log('关闭窗口')
        window.ubWindow.close()
        setTimeout(() => {
            window.ubWindow = null
        }, 100);
    })
    window.uilt.ipcRenderer.removeAllListeners('maximizeSqlWindow').on('maximizeSqlWindow', (event, data) => {
        console.log('最大化')
        if (window.ubWindow.isMaximized()) {
            window.ubWindow.restore();
        } else {
            window.ubWindow.maximize();
        }
    })
    window.uilt.ipcRenderer.removeAllListeners('minimizeSqlWindow').on('minimizeSqlWindow', (event, data) => {
        console.log('最大化')
        window.ubWindow.minimize();
    })
    // }, 500);
    // 不知道为什么不加延时前台有时候收不到消息先加上用起来先
    setTimeout(() => {
        window.uilt.ipcRenderer.sendTo(ubWindow.webContents.id, 'ping')
    }, 1000);
}
window.exports = {
    "sql": {
        mode: "none",  // 用于无需 UI 显示，执行一些简单的代码
        args: {
            // 进入插件时调用
            enter: (action) => {
                // window.utools.hideMainWindow()
                // window.utools.outPlugin()
                console.log('插件装配完成')
                // window.createWindow(
                //     './dev_index.html',
                //     { title: '11' }
                // )
                // if (IntervalEvent) {
                //     clearInterval(IntervalEvent)
                // }
                if (window.IntervalEvent) {
                    clearInterval(window.IntervalEvent)
                }
                if (window.ubWindow) {
                    registerEvent();
                    // 定时重新注册监听，防止监听时间太长导致监听失效无法关闭和放大缩小窗口问题
                    window.IntervalEvent = setInterval(() => {
                        registerEvent();
                    }, 10000)
                    window.ubWindow.show()
                    return;
                }
                createWindow();
                registerEvent();
                window.IntervalEvent = setInterval(() => {
                    registerEvent();
                }, 10000)
                // window.uilt.ipcRenderer.on('initResult', (event, data) => {
                //     console.log(2323223)
                // })
                console.log(window.ubWindow)
                // window.utools.ubrowser
                // .webPreferences( {
                //     preload: "preload.js",
                // })
                //     .device('HUAWEI Mate30')
                //     .goto('http://localhost:8080/')
                //     .devTools()
                //     .run({ minHeight: 800, minWidth: 1400, height: 750, resizable: true })
            }
        }
    }
    , 'resql': {
        mode: "none",  // 用于无需 UI 显示，执行一些简单的代码
        args: {
            enter: (action) => {
                if (window.ubWindow) {
                    window.ubWindow.close()
                }
                window.ubWindow = null
                createWindow();
                if (window.IntervalEvent) {
                    clearInterval(window.IntervalEvent)
                }
                registerEvent();
                window.IntervalEvent = setInterval(() => {
                    registerEvent();
                }, 10000)
            }
        }
    }
}